package com.turing.post.datatranfer.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.cardmanagement.util.MakeCardControl;
import com.turing.post.cardmanagement.util.MakeCardControlDao;
import com.turing.post.common.util.ContrastDao;
import com.turing.post.datatranfer.util.CardControlBusiness;
import com.turing.post.datatranfer.util.CardControlDao;
import com.turing.post.datatranfer.util.TranferApplicationException;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 卡控制业务实现接口
 * 
 * @author WZD
 */
@Stateless(mappedName = "datatranfer.impl.CardControlBusinessImpl")
@Remote
@Local
public class CardControlBusinessImpl implements CardControlBusiness {

	@EJB(mappedName = "datatranfer.impl.CardControlDaoImpl")
	private CardControlDao cardControlDao;
	@EJB(mappedName = "cardmanagement.impl.MakeCardControlDaoImpl")
	private MakeCardControlDao makeCardControlDao;// 卡控制接口

	@EJB(mappedName = "common.impl.ContrastDaoImpl")
	private ContrastDao contrastDaoImpl;// 对照表接口
	public void insertMakeCardControl(String issuecompanyno,
			String cardManufactureId, String cardFaceId)
			throws PCPSApplicationException {
		List<Object[]> list = cardControlDao.getCardControlList();
		MakeCardControl makecardcontrol = null;
		Object[] obj = null;
		for (int i = 0; i < list.size(); i++) {
			obj = list.get(i);
			String issueBranchno = contrastDaoImpl.getMapIssueBranchByNo(obj[1]
					.toString());
			//System.out.println(issueBranchno);
			// 检查发卡商机构是否存在
			if (PCPSUtil.isEmpty(issueBranchno)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "发卡商机构不存在!");
			}
			String userid = contrastDaoImpl.getMapSysOperuserByNo(obj[2]
					.toString());

			// 检查用户是否存在
			if (PCPSUtil.isEmpty(userid)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "操作员不存在!");
			}

			String cardClassicid = contrastDaoImpl.getMapCardClassicByNo(obj[5]
					.toString());
			// 检查卡种类是否存在
			if (PCPSUtil.isEmpty(cardClassicid)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "卡种类不存在!");
			}
			if (PCPSUtil.isEmpty(cardManufactureId)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "制卡商为空!");
			}
			if (PCPSUtil.isEmpty(cardFaceId)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "卡面样为空!");
			}
			String batchNo = issuecompanyno + obj[0].toString();
			if(makeCardControlDao.getMakeCardControlByBatchNo(batchNo)==null){
				makecardcontrol = new MakeCardControl();
				makecardcontrol.setBatchNo(batchNo);
				makecardcontrol.setReason("旧系统导入");
				makecardcontrol.setIssueCompanyNo(issuecompanyno.trim());
				makecardcontrol.setIssueBranchNo(issueBranchno.trim());
				makecardcontrol.setOperNo(new BigInteger(userid.trim()));
				makecardcontrol.setStartTime(PCPSUtil.getDateByString(obj[3]
						.toString()));
				makecardcontrol.setEndTime(obj[4] == null ? null : PCPSUtil
						.getDateByString(obj[4].toString()));
				makecardcontrol.setCardManufactureId(new BigInteger(
						cardManufactureId));// 制卡商
				makecardcontrol.setCardFaceId(new BigInteger(cardFaceId.trim()));
				makecardcontrol.setCardCount(Integer.parseInt(obj[7]==null?"0":obj[7].toString()));
				makecardcontrol.setExpired(3);
				makecardcontrol.setExpiredUnit("Y");
				makecardcontrol.setExpiredAlgorithm("0");
				makecardcontrol.setAlreadyInStockCount(Integer.parseInt(obj[7]==null?"0":obj[7].toString()));
				makecardcontrol.setStatus(obj[9].toString());
				int CARDPARVALUE=Integer.parseInt(obj[6]==null?"0":obj[6].toString());
				makecardcontrol.setHintInfo("只可售为"+CARDPARVALUE/100+"元卡");

				makecardcontrol.setOrderNumber(batchNo);
				makecardcontrol.setMediaType("0");
				makeCardControlDao.insertMakeCardControl(makecardcontrol);
				if(contrastDaoImpl.getMapbatchnoValueByBranchno(batchNo)==0){
					contrastDaoImpl.insertMapbatchnoValue(batchNo, CARDPARVALUE);
				}
			}
		}
	}
}
